//
//  EGTwodemensionController.m
//  Orchard
//
//  Created by 黄勇锋 on 16/8/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "EGTwodemensionController.h"
#import <AVFoundation/AVFoundation.h>


@interface EGTwodemensionController()<AVCaptureMetadataOutputObjectsDelegate>

@property (strong, nonatomic)   AVCaptureInput  *input;

@property (strong, nonatomic)   AVCaptureMetadataOutput *output;

@property (strong, nonatomic)   AVCaptureSession    *session;

@property (strong,nonatomic) UIImageView *areaImageView;

@property (strong,nonatomic) UIImageView *lineImageView;


@end



@implementation EGTwodemensionController

- (void)viewDidLoad{
    
    [super viewDidLoad];
    
    [self setupUI];
    
    [self setupSession];
    
}

- (void)setupSession{
    
    // 1. 创建输入
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    self.input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
    
    // 2. 创建输出
    self.output = [AVCaptureMetadataOutput new];
    
    // 3. 创建session 关联输入和输出
    self.session = [AVCaptureSession new];
    if([self.session canAddInput:self.input]) {
        [self.session addInput:self.input];
    }
    if([self.session canAddOutput:self.output]) {
        [self.session addOutput:self.output];
    }
    
    // 3.1 设置输出的代理（注意：必须在添加到session）
    self.output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
    [self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    // 4. 创建预览
    AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
    // 注意需要设置layer大小
    layer.frame = self.view.bounds;
    // 注意如果界面中有控件，控件会被覆盖，所以用insert插入最下面
    [self.view.layer insertSublayer:layer atIndex:0];
    
    // 5. 开始会话
    [self.session startRunning];
    
    [self startAnimation];
    
}


#pragma mark - 设置动画
- (void)startAnimation {
    
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position.y"];
    anim.fromValue = @(self.lineImageView.frame.origin.y);
    anim.toValue = @(self.areaImageView.frame.origin.y + self.areaImageView.frame.size.height - self.lineImageView.frame.size.height);
    anim.duration = 1.5;
    anim.repeatDuration = FLT_MAX;//MAXFLOAT;
    [self.lineImageView.layer addAnimation:anim forKey:nil];
    
}


#pragma mark - AVCaptureMetadataOutputObjectsDelegate -
// 处理我们的图像分析后的数据
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
    
    
    for (AVMetadataMachineReadableCodeObject *obj in metadataObjects) {
        NSURL *url = [NSURL URLWithString:obj.stringValue];
        [[UIApplication sharedApplication] openURL:url];
        //        NSLog(@"%@",obj.stringValue);
    }
}

#pragma mark - setupUI
- (void)setupUI{
    
    self.view.backgroundColor = [UIColor blackColor];
    
    //添加控件
    [self.view addSubview:self.areaImageView];
    [self.view addSubview:self.lineImageView];
    //设置约束
    [self.areaImageView makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.equalTo(self.view);
    }];
    
    [self.lineImageView makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.areaImageView).offset(10);
    }];
}


#pragma mark - 懒加载
- (UIImageView *)areaImageView{
    
    if (_areaImageView == nil) {
        _areaImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"pick_bg"]];
    }
    
    return _areaImageView;
}

- (UIImageView *)lineImageView{
    
    if (_lineImageView == nil) {
        _lineImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"line"]];
    }
    return _lineImageView;
}


@end
